package com.antexpress.bean;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Andy
 */
public class FormatValidate {
    
    /*是否满18岁*/
    public static boolean isBirthAllowed(Date date){
        Date now = new Date();
        now.setYear(now.getYear()-18);
        if(date.before(now))
            return true;
        else
            return false;
    }
    
    /*用户名字是否太短*/
    public static boolean isNameTooShort(String name) {
        if(name==null || name.length()<=0)
            return true;
        else
            return false;
    }
    
    /*用户名字是否太长*/
    public static boolean isNameTooLong(String name) {
        if(name==null||name.length()>50)
            return true;
        else
            return false;
    }
    
    /*性别是否符合规范m或f*/
    public static boolean isGender(String gender){
        if(gender.equalsIgnoreCase("m")||gender.equalsIgnoreCase("f"))
            return true;
        else
            return false;
    
    }
    
    public static boolean isPhoneLegal(String str) {  
        return isChinaPhoneLegal(str) || isHKPhoneLegal(str);  
    }  
  
    public static boolean checkEmail(String email){
        boolean flag = false;
        try{
                String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
                Pattern regex = Pattern.compile(check);
                Matcher matcher = regex.matcher(email);
                flag = matcher.matches();
            }catch(Exception e){
                flag = false;
            }
        return flag;
    }
    
    /** 
     * 大陆手机号码11位数，匹配格式：前三位固定格式+后8位任意数 
     * 此方法中前三位格式有： 
     * 13+任意数 
     * 15+除4的任意数 
     * 18+除1和4的任意数 
     * 17+除9的任意数 
     * 147 
     */  
    public static boolean isChinaPhoneLegal(String str){  
        String regExp = "^((13[0-9])|(15[^4])|(18[0,2,3,5-9])|(17[0-8])|(147))\\d{8}$";  
        Pattern p = Pattern.compile(regExp);  
        Matcher m = p.matcher(str);  
        return m.matches();  
    }  
  
    /** 
     * 香港手机号码8位数，5|6|8|9开头+7位任意数 
     */  
    public static boolean isHKPhoneLegal(String str) {  
        String regExp = "^(5|6|8|9)\\d{7}$";  
        Pattern p = Pattern.compile(regExp);  
        Matcher m = p.matcher(str);  
        return m.matches();  
    }  
    
    public static boolean checkUsername(String username){
        Pattern p = Pattern.compile("[a-zA-Z]{1}[a-zA-Z0-9]{5,17}");
        Matcher m = p.matcher(username);
        return m.matches();
    }
    
    public static boolean checkPassword(String password){
        if(password.length()>=6&&password.length()<=12)
            return true;
        else 
            return false;
    }
    
    public static boolean checkEmpStatus(int status){
        if(status==-10||status==-2||status==-1||status==0||status==1)
            return true;
        else
            return false;
    }
}
